﻿// ===============================================================================
// DotNet.Platform 开发框架 2016 版权所有
// ===============================================================================

using System.Web.Mvc;
using DotNet.Auth.Entity;
using DotNet.Auth.Utility;
using DotNet.Extensions;
using DotNet.Helper;
using DotNet.Mvc;
using DotNet.Utility;
using System.Data;
using System.Collections.Generic;
using DotNet.Entity;
using DotNet.Doc;

namespace DotNet.Web.Areas.AuthWeb.Controllers
{
    [Authorize]
    public class AuthController : JsonController
    {
        /// <summary>
        /// 当前登录用户主键
        /// </summary>
        public static string CurrentUserId
        {
            get { return CurrentUser.Id; }
        }

        /// <summary>
        /// 当前登录用户账号
        /// </summary>
        public static string CurrentUserAccount
        {
            get { return CurrentUser.Account; }
        }

        /// <summary>
        /// 当前登录用户姓名
        /// </summary>
        public static string CurrentUserName
        {
            get { return CurrentUser.Name; }
        }

        /// <summary>
        /// 当前登录用户部门主键
        /// </summary>
        public static string CurrentDepartmentId
        {
            get { return CurrentDepartment.Id; }
        }

        /// <summary>
        /// 当前登录用户部门名称
        /// </summary>
        public static string CurrentDepartmentName
        {
            get { return CurrentDepartment.Name; }
        }

        /// <summary>
        /// 当前登录会话用户对象
        /// </summary>
        public static SessionUser CurrentSessionUser
        {
            get { return AuthHelper.GetSessionUser(); }
        }

        /// <summary>
        /// 当前登录用户对象
        /// </summary>
        public static User CurrentUser
        {
            get { return CurrentSessionUser.User; }
        }

        /// <summary>
        /// 当前登录用户部门对象
        /// </summary>
        public static Department CurrentDepartment
        {
            get { return CurrentSessionUser.Department; }
        }

        /// <summary>
        /// 获取分页条件
        /// </summary>
        public PaginationCondition PageInfo()
        {
            var pageIndex = WebHelper.GetFormString("page").ToInt(1);
            var pageSize = SystemSetting.GridPageSize;
            var orderName = WebHelper.GetFormString("order");
            var orderDir = WebHelper.GetFormString("dir");
            return new PaginationCondition(pageIndex, pageSize, orderName, orderDir);
        }

        /// <summary>
        /// 数据导出
        /// </summary>
        /// <typeparam name="T">实体类型</typeparam>
        /// <param name="dataList">数据</param>
        /// <param name="fileName">导出文件名称(不太后缀名)</param>
        /// <returns></returns>
        protected FileContentResult Export<T>(IEnumerable<T> dataList, string fileName = null) where T : class, new()
        {
            var metadata = EntityMetadata.ForType(typeof(T));
            if (string.IsNullOrEmpty(fileName))
            {
                fileName = metadata.TableInfo.Caption;
            }
            byte[] fileContents = ExcelHelper.Export(dataList);
            return File(fileContents, "application/vnd.ms-excel", $"{fileName}.xlsx");
        }

        /// <summary>
        /// 数据导出
        /// </summary>
        /// <typeparam name="T">实体类型</typeparam>
        /// <param name="table">数据</param>
        /// <param name="fileName">导出文件名称(不太后缀名)</param>
        /// <returns></returns>
        protected FileContentResult ExportDataTable<T>(DataTable table, string fileName = null) where T : class, new()
        {
            var metadata = EntityMetadata.ForType(typeof(T));
            if (string.IsNullOrEmpty(fileName))
            {
                fileName = metadata.TableInfo.Caption;
            }
            byte[] fileContents = ExcelHelper.Export(table, metadata);
            return File(fileContents, "application/vnd.ms-excel", $"{fileName}.xlsx");
        }
    }
}